<?php
namespace app\admin\model\index;
use think\Model;
use think\facade\Db ;
use app\admin\common;
class Admin extends Model {
	private $table_obj;
	
	public function getAdmin($where,$field='*'){
        $data=$this->where($where)->field($field)->find();
        if($data){
            return $data->toArray();
        }else{
            return false;
        }

		return $data;
	}
	
	public function updateAdmin($up,$where){
		common::delete("admin");
		$admin=$this->find($where);
		foreach($up as $k=>$v){
			$admin->$k=$v;
		}
		return $admin->save();
	}
	
	public function insertAdminData($data){
		common::delete("admin");
		$a = $this->save($data);
		return $a->id;
	}
	
	public function selectAdminData($where,$order,$limit){
		extract(common::get('admin',[$where,$order,$limit,__function__]));
		if(empty($data)){
			$data=$this->where($where)->limit(current($limit),next($limit))->order($order)->select();
			if($data){
				common::set($data->toArray());
				return $data->toArray();
			}
		}
		return $data;
	}
	public function selectAdminDataCount($where){
		extract(common::get('admin',[$where,__function__]));
		if(empty($data)){
			$data=$this->where($where)->field("count(id) num")->find();
			common::set($data);
		}
		return $data;
	}
	/*
	 * 给用户绑定角色
	 * 
	 */
	public function bindAdminRoles($data,$where){
		common::delete("admin");
		return $this->updateAdmin($data,$where);
	}

	
	//查询分页得出分页数据
	public function selectRolePage($where,$limit,$param){
		extract(common::get('admin',[$where,__function__]));
		if(empty($data)){
			$data =Db::name('admin')->where($where);
			common::set($data);
			return $data->render();
		}
		return $data;
	}
	
	//查询单条数据
	public function findAdminData($where,$field="*"){
		extract(common::get('admin',[$where,__function__]));
		if(empty($data)){
			$data=Db::name("admin")->where($where)->field($field)->find();
			common::set($data);
		}
		return $data;
	}
}
?>